package com.authstr.serve;

import com.authstr.entity.DatabaseParameter;
import com.authstr.entity.OperationSetting;
import com.authstr.support.BuildSql;
import com.authstr.support.CompareResult;
import com.authstr.support.CompareTool;
import com.authstr.support.CreateAnnotation;
import com.authstr.support.JdbcConnectionUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class DataSync {
    public static List<String> dataSync(DatabaseParameter newConn, DatabaseParameter oldConn, OperationSetting setting){
        List<String> res=new ArrayList<>();

        List<Map<String,Object>> new_res = new ArrayList<>();
        List<Map<String,Object>> old_res = new ArrayList<>();
        try {
            JdbcConnectionUtil conn99 = new JdbcConnectionUtil(
                    newConn.getUsername(),
                    newConn.getPassword(),
                    newConn.getLink_url());
            String sql =setting.getNewSql();
            new_res=conn99.getQueryResult(sql);
            conn99.close();


            //获取57查询结果
            JdbcConnectionUtil conn57 = new JdbcConnectionUtil(
                    oldConn.getUsername(),
                    oldConn.getPassword(),
                    oldConn.getLink_url());
            String sql2 =setting.getOldSql();
            old_res=conn57.getQueryResult(sql2);
            conn57.close();

        }catch(Exception e){
            e.printStackTrace();
        }

        CompareResult result= CompareTool.compare(new_res,old_res,setting.getUniqueColumn());
        result.setUpdateIgnoreColumn(setting.getUpdateIgnoreColumn());
        result.setUpdateIgnoreColumn(setting.getUpdateIgnoreColumn());

        BuildSql buildSql=new BuildSql(setting.getTableName(),result.getUniqueColumn());
        buildSql.setEnableAddAnnotation(setting.getEnableAddAnnotation());

        buildSql.setEnableDelectByIdOnInsert(setting.getEnableDelectByIdOnInsert());
        buildSql.setEnableDelectByUniqueOnInsert(setting.getEnableDelectByUniqueOnInsert());
        buildSql.setEnableUserUUIDOnInsert(setting.getEnableUserUUIDOnInsert());

        buildSql.setEnableUpdateSameOnUpdate(setting.getEnableUpdateSameOnUpdate());
        buildSql.setEnableDelectByIdOnUpdate(setting.getEnableDelectByIdOnUpdate());
        buildSql.setEnableDelectByUniqueOnUpdate(setting.getEnableDelectByUniqueOnUpdate());
        buildSql.setEnableChangeInsertOnUpdate(setting.getEnableChangeInsertOnUpdate());
        buildSql.setEnableUserUUIDOnUpdate(setting.getEnableUserUUIDOnUpdate());

        buildSql.setEnableUserUniqueDelete(setting.getEnableUserUniqueDelete());


        String[] uniqueColumn=result.getUniqueColumn();

        if(setting.getEnableCreateInsert()){
            res.add("-- 需要插入"+result.getNewSubtractOld().size()+"条数据");
        }
        if(setting.getEnableCreateUpdate()){
            CompareTool.compareGetUpdateData(result);
            res.add("-- 需要更新"+result.getActualNeedUpdateValue().size()+"条");
        }
        if(setting.getEnableCreateDelete()){
            res.add("-- 需要删除"+result.getOldSubtractNew().size()+"条");
        }


        if(setting.getEnableCreateInsert()){
            for(String key :result.getNewSubtractOld()){

                String[] uniqueValue=new String[uniqueColumn.length];
                Map<String,Object> currData=result.getNewDate().get(key);

                for(int i=0;i<uniqueColumn.length;i++){
                    uniqueValue[i]=currData.get(uniqueColumn[i]).toString();
                }
                List<String> build_sql=buildSql.createInsertSql(uniqueValue,currData);
                res.addAll(build_sql);
                res.add("");

            }
        }
        res.add("");
        res.add("");
        res.add("");
        res.add("");
        if(setting.getEnableCreateUpdate()){
            for(String key :result.getActualNeedUpdateValue()){
                String[] uniqueValue=new String[uniqueColumn.length];
                Map<String,Object> newData=result.getNewDate().get(key);
                Map<String,Object> oldData=result.getOldDate().get(key);

                for(int i=0;i<uniqueColumn.length;i++){
                    uniqueValue[i]=newData.get(uniqueColumn[i]).toString();
                }
                List<String> build_sql=buildSql.createUpdateSql(uniqueValue,result.getActualNeedUpdateColumn(),newData,oldData);
                res.addAll(build_sql);
                res.add("");
            }
        }
        res.add("");
        res.add("");
        res.add("");
        res.add("");
        if(setting.getEnableCreateDelete()){
            for(String key :result.getOldSubtractNew()){
                String[] uniqueValue=new String[uniqueColumn.length];
                Map<String,Object> currData=result.getOldDate().get(key);

                for(int i=0;i<uniqueColumn.length;i++){
                    uniqueValue[i]=currData.get(uniqueColumn[i]).toString();
                }
                List<String> build_sql=buildSql.createDeleteSql(uniqueValue,currData);
                res.addAll(build_sql);
                res.add("");
            }
        }

        CreateAnnotation.printListString(res);
        return res;
    }
}
